package com.qiaoyanqing.ximalaya.discovermodule.discover_fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qiaoyanqing.ximalaya.discovermodule.discover_adapter.Discover_Recommend_HeadViewpagerAdapter;
import com.qiaoyanqing.ximalaya.R;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverFragment extends Fragment {
    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private List<Fragment> mData;

    public DiscoverFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_discover, container, false);
        mTabLayout = (TabLayout) view.findViewById(R.id.discover_tablayout);
        mViewPager = (ViewPager) view.findViewById(R.id.discover_viewpager);
        //向ViewPager中添加Fragment
        initData();
        //向TabLayout中添加数据
        initTabLayout();
        //viewpager设置Adapter
        initViewpager();
        //设置viewpager与Tablayout的相互监听，我这里没有用到老师的setupwith
        //而是两个相互监听的方法
        initListener();

        return view;

    }

    private void initListener() {
        mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
        mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
    }

    private void initViewpager() {
        Discover_Recommend_HeadViewpagerAdapter discoverRecommendHeadViewpagerAdapter = new Discover_Recommend_HeadViewpagerAdapter(getFragmentManager(), mData);
        mViewPager.setAdapter(discoverRecommendHeadViewpagerAdapter);

    }

    private void initTabLayout() {
        String[] title = getResources().getStringArray(R.array.title);
        for (int i = 0; i < title.length; i++) {
            TabLayout.Tab tab = mTabLayout.newTab();
            tab.setText(title[i]);
            mTabLayout.addTab(tab);
        }
        mTabLayout.setTabMode(TabLayout.MODE_FIXED);
        mTabLayout.setTabTextColors(Color.BLACK, Color.RED);
    }

    private void initData() {

        mData = new ArrayList<>();

        DiscoverRecommendFragment discoverRecommendFragment = new DiscoverRecommendFragment();
        Discover_AnchorFragment discoverAnchorFragment = new Discover_AnchorFragment();
        Discover_BroadcastFragment discoverBroadcastFragment = new Discover_BroadcastFragment();
        Discover_ClassifyFragment discoverClassifyFragment = new Discover_ClassifyFragment();
        DiscoverRankFragment discoverRankFragment = new DiscoverRankFragment();

        mData.add(discoverRecommendFragment);
        mData.add(discoverClassifyFragment);
        mData.add(discoverBroadcastFragment);
        mData.add(discoverRankFragment);
        mData.add(discoverAnchorFragment);


    }

}
